﻿using ManagementTools.Service;
using ManagementTools.Service.Dto;
using MiniExcelLibs;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ManagementTools
{
    public partial class FrmImportDictionaryMakeEnum : Form
    {
        private MainForm? mParent;
        private DicToEnumService mDicToEnumService;
        private List<DicTypeXto> mDicTypes;
        private string mSrcFile;
        private List<string> mSheetNames;

        public FrmImportDictionaryMakeEnum()
        {
            InitializeComponent();
            mDicToEnumService = new DicToEnumService(App.SugarClient);
        }
      
        private void FrmImportDictionaryMakeEnum_Load(object sender, EventArgs e)
        {
            if (Tag != null) mParent = Tag as MainForm;
        }

        private void btnOpenFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog()
            {
                Title = "打开字典数据文件",
                Filter = "Excel 文件(*.xlsx)|*.xlsx|全部文件(*.*)|*.*",
                RestoreDirectory = true,
                Multiselect=false
            };
            if(fd.ShowDialog() == DialogResult.OK)
            {
                mSrcFile = fd.FileName;
                mSheetNames = MiniExcel.GetSheetNames(fd.FileName);
                if(!mSheetNames.Contains("字典类型"))
                {
                    MessageBox.Show("字典文件的第一页应该是'字典类型', 并至少包含1个以上的字典数据页。\r\n数据页的页签名称应该对应字典类型的唯一编码","错误：指定的文件不是字典文件",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    return;
                }
                // var rows =  MiniExcel.Query(fd.FileName, sheetName: "SheetName");
                mDicTypes = MiniExcel.Query<DicTypeXto>(fd.FileName, sheetName: "字典类型").ToList();
                dgvDic.Columns.Clear();
                dgvDic.DataSource = mDicTypes;
                
               
            }
        }

        private void dgvDic_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvDic.CurrentRow == null) return;
            dgvValue.Columns.Clear();
            var dic = mDicTypes[dgvDic.CurrentRow.Index];
            if (mSheetNames.Contains(dic.Code))
            {
                var dicData = MiniExcel.Query<DicDataXto>(mSrcFile, sheetName: dic.Code).ToList();
                dgvValue.DataSource = dicData;
            }
            else
            {
                if (mParent != null) mParent.ShowStaInfo($"未读取到{dic.Code}页数据");
            }
        }

        private void txtTargPath_DoubleClick(object sender, EventArgs e)
        {
            FolderBrowserDialog fb = new FolderBrowserDialog
            {
                Description = "选择枚举文件(*.cs)的输出路径，可以指向项目的枚举目录，也可以指向一个暂存目录",
                ShowNewFolderButton = true,
            };
            if(fb.ShowDialog()== DialogResult.OK)
            {
                txtTargPath.Text = fb.SelectedPath;
            }
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            if (dgvDic.SelectedRows.Count == 0)
            {
                MessageBox.Show("请先选择字典", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (string.IsNullOrEmpty(txtTargNamespace.Text))
            {
                MessageBox.Show("必须指定域名空间", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (mParent != null) mParent.ShowStaInfo("开始转换字典");
            foreach (DataGridViewRow row in dgvDic.SelectedRows)
            {
                var dic = mDicTypes[row.Index];
                var typeId = mDicToEnumService.InsertDictType(dic);
                if (typeId > 0)
                {
                    if (mParent != null) mParent.AppendEcho($"保存{dic.Name}成功");
                    var dicDatas = MiniExcel.Query<DicDataXto>(mSrcFile, sheetName: dic.Code).ToList();
                    if(mDicToEnumService.InsertDictData(typeId, dicDatas) > 0)
                    {
                        if (mParent != null) mParent.AppendEcho($"保存{dic.Name}数据成功");
                        if( mDicToEnumService.DictionaryToEnum(dic.Code, txtTargPath.Text, txtTargNamespace.Text))
                        {
                            if (mParent != null) mParent.AppendEcho($"生成{dic.Name}对应的枚举文件成功");
                        }
                        else
                        {
                            MessageBox.Show($"生成字典: '{dic.Name}'对应的枚举文件失败，错误信息为\r\n{mDicToEnumService.ErrMsg}","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
                            return;
                        }
                    }
                    else
                    {
                        MessageBox.Show($"生成字典: '{dic.Name}'对应的数据失败，错误信息为\r\n{mDicToEnumService.ErrMsg}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
                else
                {
                    MessageBox.Show($"保存字典： '{dic.Name}'失败，错误信息为\r\n{mDicToEnumService.ErrMsg}","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    return;
                }
            }
        }
    }
}
